// pages/cart/index.js
Page({

  /**
   * 页面的初始数据
   */
  //计算按钮个数计算
  settlement() {
    let sum = this.data.cart.filter(item => item.checked == true)
    this.setData({
      sum: sum.length
    })
  },
  //总计计算
  total() {
    let sums = this.data.cart.filter(item => item.checked == true)
    let sum = sums.reduce((prev, cur, index) => {
      if (cur.goods_price == 0) return prev + 4999
      return prev + cur.goods_price * cur.num
    }, 0)
    this.setData({
      totals: sum
    })
  },
  //点击按钮获取用户地址信息
  getAddress() {
    wx.getSetting({
      success: (res) => {
        let isTrue = res.authSetting
        if (JSON.stringify(isTrue) == "{}" || isTrue["scope.address"] == true) {
          wx.chooseAddress({
            success: (res) => { wx.setStorageSync('address', res) }
          })
        } else {
          wx.openSetting({
            success: (res) => { console.log(res); },
          })
        }
      }
    })
  },
  //商品选项被点击时
  clickCommodity(e) {
    //获取谁被点击
    let index = e.currentTarget.dataset.index
    let url = `cart[${index}].checked`
    let urls = this.data.cart[index].checked
    this.setData({
      [url]: !urls
    })
    //看看是否全选了
    let allCheck = this.data.cart.every(item => item.checked)
    this.setData({
      allCheck,
    })
    //计算结算数量
    this.settlement()
    //计算总结
    this.total()
  },
  //全选按钮被点击
  selectAll(e) {
    let isAllCheck = this.data.allCheck
    if (isAllCheck) {
      this.data.cart.forEach((item, index) => {
        let url = `cart[${index}].checked`
        this.setData({
          [url]: !isAllCheck
        })
      })
    } else {
      this.data.cart.forEach((item, index) => {
        let url = `cart[${index}].checked`
        this.setData({
          [url]: !isAllCheck
        })
      })
    }
    //改变自己
    this.setData({
      allCheck: !this.data.allCheck
    })
    //计算结算数量
    this.settlement()
    //计算总结
    this.total()
  },
  //点击-，+按钮
  changeButton(e) {
    let index = e.currentTarget.dataset.index
    let type = e.currentTarget.dataset.type
    let url = `cart[${index}].num`
    let urls = this.data.cart[index].num
    //type,1为减，2为加
    if (type == 1) {
      if (this.data.cart[index].num > 1) {
        this.setData({
          [url]: urls - 1
        })
      } else {
        wx.showModal({
          title: `是否删除此商品`,
          success: () => {
            let cart = this.data.cart.filter((item, indexs) => indexs != index)
            this.setData({
              cart,
            })
            wx.showToast({
              title: '删除成功',
            })
            //计算结算数量
            this.settlement()
            //计算总结
            this.total()
          }
        })
      }
    } else if (type == 2) {
      this.setData({
        [url]: urls + 1
      })
    }
    //计算结算数量
    this.settlement()
    //计算总结
    this.total()
  },
  //结算
  check(){
    //判断是否有地址
    if(this.data.isShowButtton){
      wx.showToast({
        title: '请选择收货地址',
        icon:'none'
      })
      return
    }
    //判断是否有勾选商品
    if(this.data.sum==0){
      wx.showToast({
        title: '请选择需要购买的商品',
        icon:'none'
      })
      return
    }
    wx.navigateTo({
      url: '../pay/index',
    })
  },
  data: {
    isShowButtton: true,//是否显示按钮
    addressData: {},//地址数据
    address: '',//地址
    cart: [],//购物车数据
    allCheck: false,//是否全选
    sum: 0,//结算数量
    totals: 0,//总计
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取本地存储的地址
    let Address = wx.getStorageSync('address')
    if (Address) {
      this.setData({
        isShowButtton: false,
        addressData: Address,
        address: Address.provinceName + Address.cityName + Address.countyName + Address.detailInfo
      })
    }
    //获取本地购物车数据
    let cart = wx.getStorageSync('cart') || []
    //所有商品默认不选中
    cart.forEach((item, index) => {
      if (!item.checked) item.checked = false
    })
    let allCheck = cart.every(item => item.checked)
    this.setData({
      cart,
      allCheck,
    })
    //计算结算数量
    this.settlement()
    //计算总结
    this.total()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    wx.setStorageSync('cart', this.data.cart)
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    wx.setStorageSync('cart', this.data.cart)

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})